package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.meterService.bean.MsRepairBean;
import com.koron.css2.meterService.mapper.MsRepairMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.ValidationKey;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 校表修改
 * @author 周利斌
 *
 */
public class MsRepairUpdate implements ServerInterface{

	private static Logger log = LogManager.getLogger(MsRepairUpdate.class);

	@Override
    @ValidationKey(clazz = MsRepairBean.class,method = "update")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Map> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Map.class);
		try {
			MsRepairBean bean = JsonUtils.objectToPojo(req.getData(), MsRepairBean.class);
			if(StringUtils.isBlank(bean.getId())) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("id不能为空");
				return info;
			}
			MsRepairMapper mapper = factory.getMapper(MsRepairMapper.class);
			bean.setUpdateInfo(userInfo);
			int iCount = mapper.updateMsRepair(bean);
			if(iCount < 1) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("修改失败");
				return info;
			}
			Map<String, String> data = new HashMap<String, String>();
			data.put("id", bean.getId());
			info.setData(data);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("修改失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}

}
